﻿// ***********************************************************************
// Provided for Informational Purposes Only
//
// Apache 2.0 License
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.WinRT
// File      : JsonDotNetSerializer.cs
//
// ***********************************************************************
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace ContractMeow.WinRT.Serialization
{
	/// <summary>
	/// Class JsonDotNetSerializer
	/// </summary>
	public class JsonDotNetSerializer : ISerializer
	{
		/// <summary>
		/// Gets the text encoding.
		/// </summary>
		/// <value>The text encoding.</value>
		public Encoding TextEncoding { get; protected set; }

		/// <summary>
		/// Gets the converters.
		/// </summary>
		/// <value>The converters.</value>
		public IList<JsonConverter> Converters { get; protected set; }

		/// <summary>
		/// Gets or sets the settings.
		/// </summary>
		/// <value>The settings.</value>
		protected JsonSerializerSettings Settings { get; set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="JsonDotNetSerializer" /> class.
		/// </summary>
		/// <param name="encoding">The encoding.</param>
		/// <param name="converters">The converters.</param>
		public JsonDotNetSerializer(Encoding encoding = null, params JsonConverter[] converters)
		{
			TextEncoding = encoding ?? Encoding.UTF8;
			Converters = converters;
			Settings = new JsonSerializerSettings
				{
					NullValueHandling = NullValueHandling.Ignore,
					ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
					Converters = Converters
				};
		}

		/// <summary>
		/// Gets the type of the content.
		/// </summary>
		/// <value>The type of the content.</value>
		public String ContentType
		{
			get { return "application/json"; }
		}

		/// <summary>
		/// Serializes the specified obj.
		/// </summary>
		/// <param name="obj">The obj.</param>
		/// <returns>Byte[][].</returns>
		public Byte[] SerializeToByteArray(Object obj)
		{
			return TextEncoding.GetBytes(Serialize(obj));
		}

		/// <summary>
		/// Serializes the specified <see cref="Object"/>
		/// </summary>
		/// <param name="obj">The <see cref="Object"/> to serialize</param>
		/// <returns></returns>
		public String Serialize(Object obj)
		{
			return JsonConvert.SerializeObject(obj, Formatting.None, Settings);
		}

	}
}